package com.dsbj.mp.icardservice.controller.msgcenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.mp.icardservice.entity.msgcenter.McCate;
import com.dsbj.mp.icardservice.entity.msgcenter.McMessage;
import com.dsbj.mp.icardservice.service.msgcenter.McMessageService;
import com.dsbj.mp.icardservice.utils.strprocess.ObjectParse;
import com.dsbj.mp.icardservice.utils.strprocess.StrUtil;
import com.dsbj.mp.icardservice.utils.strprocess.Time_convert;
import javafx.beans.property.ObjectProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.*;

/**
 * 消息中心》输出消息。
 * @author cjw 2020-02-21
 */
@Service
@Transactional
public class OutputMcMessage {
	
	@Resource
	private McMessageService mcMessageService;
	
	
	
	/**
	 * 输出list_mcCate信息。
	 * <pre>
	 *     输出格式为:[
	 *     			{
	 *     			 "mcCateId": "mcCateId",
	 *     			 "mcCateName": "mcCateName",
	 *     			 "sort": 1,
	 *     			 "preview":"preview",
	 *     			 "createTime":"createTime",
	 *     			 "numNotRead": 1
	 *     			},
	 *     			...
	 *     ]
	 * </pre>
	 * @param
	 * @return 
	 * @author cjw 2020-02-21
	 */
	public List<Map<String,Object>> generateMcCate(List<McCate> list_mcCate,String idcardno){
		if(list_mcCate==null){
			return null;
		}
		
		List<Map<String,Object>> list_mapMcCate=new ArrayList<>();
		Map<String,Object> mapMcCate=null;
		String preview=null;
		int previewLimitLength=20;
		
		for (McCate mcCate: list_mcCate){
			mapMcCate=new LinkedHashMap<>();
			mapMcCate.put("mcCateId",mcCate.getId());
			mapMcCate.put("mcCateName",mcCate.getName());
			mapMcCate.put("iconUrl",mcCate.getIconUrl());
			mapMcCate.put("sort",mcCate.getSort());
			
			//preview
			//根据mcCateId获取该类别下的最新消息
			McMessage mcMessage= mcMessageService.getLatestMcMessageByMcCateId(mcCate.getId(),idcardno);
			preview= mcMessage==null? null: mcMessage.getDes();
			if(preview!=null && preview.length()>previewLimitLength){
				preview= preview.substring(0,previewLimitLength)+"...";
			}
			mapMcCate.put("preview",preview);
			mapMcCate.put("createTime", Time_convert.convert_toStrWithoutSec(mcMessage==null? null: mcMessage.getCreateTime()));
			
			//numNotRead
			List<McMessage> list_mcMessage=mcMessageService.getMcMessageByMcCateIdByStatus(mcCate.getId(),1,idcardno);
			mapMcCate.put("numNotRead",list_mcMessage.size());
			//
			list_mapMcCate.add(mapMcCate);
		}
		return list_mapMcCate;
	}
	
	/**
	 * 输出list_mcMessage信息。
	 * @param
	 * @return 
	 * @author cjw 2020-02-21
	 */
	public static List<Map<String,Object>> generateMcMessage(List<McMessage> list_mcMessage){
		if(list_mcMessage==null){
			return null;
		}
		
		List<Map<String,Object>> list_mapMcMessage=new ArrayList<>();
		Map<String,Object> mapMcMessage=null;
		int briefDesLimitLenght=20;
		
		for(McMessage mcMessage: list_mcMessage){
			mapMcMessage=new LinkedHashMap<>();
			mapMcMessage.put("mcMessageId",mcMessage.getId());
			mapMcMessage.put("title",mcMessage.getTitle());
			mapMcMessage.put("des",mcMessage.getDes());
			//brefDes
			String briefDes=mcMessage.getDes();
			if(briefDes!=null && briefDes.length()>briefDesLimitLenght){
				briefDes=briefDes.substring(0,briefDesLimitLenght)+"...";
			}
			mapMcMessage.put("briefDes",briefDes);
			mapMcMessage.put("mcCateId",mcMessage.getMccateid());
			mapMcMessage.put("status",mcMessage.getStatus());
			mapMcMessage.put("createTime",Time_convert.convert_toStrWithoutSec(mcMessage.getCreateTime()));
			mapMcMessage.put("jumpType",mcMessage.getJumpType());
			mapMcMessage.put("jumpValue",mcMessage.getJumpValue());
			//
			list_mapMcMessage.add(mapMcMessage);
		}
		return list_mapMcMessage;
	}
	
	/**
	 * 分页输出mcMessage信息。
	 * @param
	 * @return
	 * @author cjw 2020-02-21
	 */
	public static Page<Map<String,Object>> generateMcMessageByPage(Page<McMessage> page){
		if(page==null||page.getRecords()==null){
			return (Page<Map<String, Object>>)(Page)page;
		}
		
		List<Map<String,Object>> list_mapMcMessage=generateMcMessage(page.getRecords());
		Page<Map<String,Object>> pageReturn= ((Page<Map<String,Object>>)(Page)page).setRecords(list_mapMcMessage);
		return pageReturn;
	}
	
	
	
	
	
	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2020-02-22
	 */
	public static void main(String[] args){
	
	}
}
